<?php

namespace App\Observers;

use App\Models\Group;
use App\Repositories\GroupRepository;

class GroupObserver
{

    private $groupRepository;

    public function __construct(GroupRepository $groupRepository)
    {
        $this->groupRepository = $groupRepository;
    }

    /**
     * @param Group $group
     */
    public function creating(Group $group)
    {
        $group->user_id = request()->user()->id;
    }

    /**
     * @param Group $group
     */
    public function created(Group $group)
    {
        $this->groupRepository->addUser($group, request()->user());
    }

    /**
     * @param Group $group
     */
    public function updated(Group $group)
    {
        $group->removeCache();
    }

    /**
     * @param Group $group
     */
    public function saved(Group $group)
    {
        $this->updated($group);
    }

    /**
     * @param Group $group
     */
    public function deleted(Group $group)
    {
        $this->updated($group);
    }
}
